//
//  HSClassTableController.swift
//  Home_School_08
//
//  Created by 张晓林 on 2016/12/28.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSClassTableController: UIViewController {
    // 标题视图的宽高
    let lableW: CGFloat = screenW / 8
    let lableH: CGFloat = 44
    // 集合视图属性
    var collectionView: UICollectionView!
    // 保存课表数据的数据源数组
    var dataArray = [HSClassTableModel]()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // 
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        setTopNavView()
        setWeekView()
        setColectionView()
        setLeftClassTimeV()
        
        downlaodClassData()
    }
    
    // 请求课表数据
    func downlaodClassData() {
        // 获得的接口地址
        let urlStr = "ZH-schoolname-S-curriculum"
        // 设置参数
        let parm = ["key":userID]
        // 请求数据
        HSNetworkTools.shared.request(url: urlStr, parameters: parm) {[weak self] (data, err) in
            // 判断网络是否异常
            guard let data = data as? NSDictionary,let dictArr = data["data"] as? [NSDictionary] else {
                HSAlertView.bottomAlertView("网络异常")
                return
            }

            // 遍历数组
            for dict in dictArr {
                // 获得当前字典中的所有key,并排序
                let keys = (dict.allKeys as! [String]).sorted()
                // 遍历key，获得对应的字典
                for s in keys {
                    // 根据遍历的key,获得字典
                    let classDict = dict[s] as! [String: String]
                    // 字典转模型
                    let classModel = HSClassTableModel(dict: classDict)
                    // 保存到一个数组中
                    self?.dataArray.append(classModel)
                    // 处理午休数据
                    if s == "c4"{
                        let emptyM = HSClassTableModel(dict: [:])
                        self?.dataArray.append(emptyM)
                    }
                }
            }
            
            // 这里表示数据转换完成,刷新视图
            self?.collectionView.reloadData()
        }
    }
}

//MARK: - UI相关
extension HSClassTableController {
    // 顶部导航栏
    func setTopNavView() {
        // 背景view
        let navView = UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 64))
        navView.backgroundColor = UIColor.colorWithHex(hexColor: 0x009ee7)
        view.addSubview(navView)
        // 左边返回按钮
        let backBtn = UIButton(type: .custom)
        backBtn.frame = CGRect(x: 0, y: 20, width: 150, height: 44)
        backBtn.setTitle("课表", for: .normal)
        backBtn.setImage(UIImage(named: "返回按钮"), for: .normal)
        backBtn.contentHorizontalAlignment = .left
        backBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -30, bottom: 0, right: 0)
        backBtn.addTarget(self, action: #selector(backBtnClicked), for: .touchUpInside)
        navView.addSubview(backBtn)
    }
    
    // 返回按钮的事件犯法
    func backBtnClicked() {
        dismiss(animated: true, completion: nil)
    }
    
    // 添加周视图
    func setWeekView() {
        // 设置标题数组
        let weekTitles = ["","一","二","三","四","五","六","日"]
        // 循环添加
        for (i,v) in weekTitles.enumerated() {
            let l = UILabel(title: v, color: #colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1), fontSize: 15)
//            l.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            l.frame = CGRect(x: CGFloat(i) * lableW, y: 64, width: lableW, height: lableH)
            view.addSubview(l)
        }
    }
    
    // 左边课时视图
    func setLeftClassTimeV() {
        // 循环添加
        for i in 1..<10 {
            if i == 5 {
                let btn = UIButton(type: .custom)
                btn.frame = CGRect(x: 0, y: 64 + CGFloat(i) * lableH, width: screenW, height: lableH)
                btn.setBackgroundImage(UIImage(named: "curriculum_wuxiu_bg"), for: .normal)
                btn.setTitle("午     休", for: .normal)
                view.addSubview(btn)
            }else {
                let l = UILabel(title: i>4 ? "\(i-1)":"\(i)", color: #colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1), fontSize: 15)
//                l.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
                l.frame = CGRect(x: 0, y: 64 + CGFloat(i) * lableH, width: lableW, height: lableH)
                view.addSubview(l)
            }
            // 分割线
            let line = UIView(frame: CGRect(x: 5, y: 64 + CGFloat(i+1) * lableH, width: screenW - 10, height: 1))
            line.backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1)
            view.addSubview(line)
        }
    }
    
    // 显示课程内容的集合视图
    func setColectionView() {
        // 设置布局对象
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: lableW, height: lableH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        // 设置滚动方向
        layout.scrollDirection = .horizontal
        // 初始化
        let rect = CGRect(x: lableW, y: 64 + lableH, width: screenW-lableW, height: lableH * 9)
        collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
        collectionView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        // 设置代理和数据源
        collectionView.delegate = self
        collectionView.dataSource = self
        // 注册原形cell
        collectionView.register(HSClassTitleCell.self, forCellWithReuseIdentifier: "classCell")
        
        view.addSubview(collectionView)
    }
}

//MARK: - 集合视图的数据源和代理犯法
extension HSClassTableController: UICollectionViewDelegate,UICollectionViewDataSource {
    
    // 格子个数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }

    // 格子视图
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "classCell", for: indexPath) as! HSClassTitleCell
        // 获得对应的课程数据
        let classModel = dataArray[indexPath.item]
        
        cell.model = classModel
        
        return cell
    }
}















